package edu.gduf.utils;


import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import edu.gduf.domain.User;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.conn.HttpHostConnectException;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.net.URISyntaxException;


/**
 * 教务系统工具类
 * @author kgy
 */
@Repository
public class JwxtUtil {

    private static String uri = "http://jwxt.gduf.edu.cn/app.do"; // 教务系统API调用地址
    private static ObjectMapper objectMapper = null; // json处理对象
    private Object token = null; // token
    private String s = null; // response。text
    private JsonNode jsonNode = null;
    static {
        objectMapper = new ObjectMapper();
    }

    /**
     * 返回学生的信息列表
     * @param xh
     * @param pwd
     * @return json
     * @throws URISyntaxException
     */
    public JsonNode getStudentInfo(String xh,String pwd) throws URISyntaxException {
        return execute(xh,getToken(xh,pwd));
    }

    /**
     * 获取登陆token
     * @param xh
     * @param pwd
     * @return token的字符串
     * @throws URISyntaxException
     */
    public String getToken(String xh,String pwd) throws URISyntaxException {
        CloseableHttpClient httpClient = HttpClients.createDefault();
        URIBuilder uriBuilder = new URIBuilder(uri);
        // 构造请求地址
        uriBuilder.setParameter("method","authUser").setParameter("xh",xh).setParameter("pwd",pwd);
        // 构造Get请求
        HttpGet httpGet = new HttpGet(uriBuilder.build());
       // 响应模型
        CloseableHttpResponse response = null;
        try {
            // 由客户端执行Get请求
            response = httpClient.execute(httpGet);
            // 从响应模型中获取响应体
            if(response.getStatusLine().getStatusCode() == 200){
                HttpEntity entity = response.getEntity();
                s = EntityUtils.toString(entity,"utf-8");
                jsonNode = objectMapper.readTree(s);
                token = jsonNode.get("token");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            return token.toString().replace("\"","");
        }

    }

    /**
     * 执行获取用户信息
     * @param xh
     * @param token
     * @return Json
     */
    public static JsonNode execute(String xh,String token){
        CloseableHttpClient httpClient = HttpClients.createDefault();
        URIBuilder uriBuilder = null;
        try{
            uriBuilder = new URIBuilder(uri);
        }catch (URISyntaxException e){
            e.printStackTrace();
            System.out.println("URI错误！");
        }
        // 设置参数
        uriBuilder.setParameter("method","getUserInfo");
        uriBuilder.setParameter("xh",xh);
        HttpGet httpGet = null;
        try{
            //创建get请求
            httpGet = new HttpGet(uriBuilder.build());
        }catch (URISyntaxException e){
            e.printStackTrace();
            System.out.println("util中的httpget方法错误");
        }
        httpGet.setHeader("token",token);
        CloseableHttpResponse response = null;
        try{
            // 由客户端执行Get请求
            response = httpClient.execute(httpGet);
        }catch (IOException e){
            e.printStackTrace();
            System.out.println("util中的execute错误");
        }catch (Exception e){
            e.printStackTrace();
        }
        // 从响应模型中获取响应实体
        HttpEntity entity = response.getEntity();
        String s = null;
        try{
            s = EntityUtils.toString(entity,"utf-8");
        }catch (IOException e){
            e.printStackTrace();
            System.out.println("字符串转换失败！");
        }
        try {
            return objectMapper.readTree(s);
        }catch (JsonProcessingException e){
            e.printStackTrace();
            return null;
        }
    }



}
